/**
 * fileName:  FruitImpl.java
 * author:  Lei
 * Company:  Cloudland Studio
 * Createdate:  2014-6-27 下午10:50:21
 *
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2011
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2014-6-27          Lei            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.commercial.masterdata.handler.support;

import java.util.ArrayList;

import org.cloudland.commercial.masterdata.bean.FruitInfo;
import org.cloudland.commercial.masterdata.bean.FruitNutrition;
import org.cloudland.commercial.masterdata.bean.FruitStructure;
import org.cloudland.commercial.masterdata.bean.NutritionBasic;
import org.cloudland.commercial.masterdata.handler.FruitStore;
import org.cloudland.commercial.masterdata.store.PageResult;
import org.cloudland.commercial.masterdata.store.entity.FruitEntity;
import org.cloudland.commercial.masterdata.store.entity.FruitNutritionEntity;
import org.cloudland.commercial.masterdata.store.entity.NutritionEntity;
import org.cloudland.commercial.masterdata.store.entity.PageQueryEntity;
import org.cloudland.commercial.masterdata.store.read.ArgumentCostReader;
import org.cloudland.commercial.masterdata.store.read.ArgumentReader;
import org.cloudland.commercial.masterdata.store.read.EfficacyReader;
import org.cloudland.commercial.masterdata.store.read.FruitArgumentReader;
import org.cloudland.commercial.masterdata.store.read.FruitCategoryReader;
import org.cloudland.commercial.masterdata.store.read.FruitEfficacyRelationReader;
import org.cloudland.commercial.masterdata.store.read.FruitMasterReader;
import org.cloudland.commercial.masterdata.store.read.FruitNutritionReader;
import org.cloudland.commercial.masterdata.store.read.FruitStructureReader;
import org.cloudland.commercial.masterdata.store.read.NutrientReader;
import org.cloudland.commercial.masterdata.store.write.FruitArgumentWriter;
import org.cloudland.commercial.masterdata.store.write.FruitEfficacyRelationWriter;
import org.cloudland.commercial.masterdata.store.write.FruitMasterWriter;
import org.cloudland.commercial.masterdata.store.write.FruitNutritionWriter;
import org.cloudland.commercial.masterdata.store.write.FruitStructureWriter;

 /**
 * <pre> 
 * TODO 请充分描述该类提供的作用, 或与其他类会有关联的使用。
 * 使用 java.lang.Object#toString(), 或者 {@link java.lang.Character Character} 等标签
 * </pre>
 * @ClassName  FruitImpl
 * @author  Lei
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
public class FruitImpl implements FruitStore {
	
	/**
	 * fruitMasterWriter 主数据写操作
	 */
	private FruitMasterWriter fruitMasterWriter;
	
	/**
	 * fruitNutritionWriter 水果营养信息写入接口
	 */
	private FruitNutritionWriter fruitNutritionWriter;
	
	/**
	 * fruitArgumentWriter 水果动态参数写入接口
	 */
	private FruitArgumentWriter fruitArgumentWriter;
	
	/**
	 * fruitStructureWriter 水果结构信息写入接口
	 */
	private FruitStructureWriter fruitStructureWriter;
	
	/**
	 * fruitEfficacyRelationWriter 水果结构功效关系写入接口
	 */
	private FruitEfficacyRelationWriter fruitEfficacyRelationWriter;
	
	/**
	 * fruitNutritionReader 水果营养信息读取接口
	 */
	private FruitNutritionReader fruitNutritionReader;
	
	/**
	 * fruitNutritionReader 营养信息读取接口
	 */
	private NutrientReader nutritionReader;
	
	/**
	 * fruitArgumentReader 水果动态参数读取接口
	 */
	private FruitArgumentReader fruitArgumentReader;
	
	/**
	 * argumentReader 参数读取接口
	 */
	private ArgumentReader argumentReader;
	
	/**
	 * argumentCostReader 参数值读取接口接口
	 */
	private ArgumentCostReader argumentCostReader;
	
	/**
	 * fruitStructureReader 水果结构读取接口
	 */
	private FruitStructureReader fruitStructureReader;
	
	/**
	 * fruitEfficacyRelationReader 水果结构功效读取接口
	 */
	private FruitEfficacyRelationReader fruitEfficacyRelationReader;
	
	/**
	 * efficacyReader 功效读取接口
	 */
	private EfficacyReader efficacyReader;

	/**
	 * fruitMasterReader 水果主数据读取接口
	 */
	private FruitMasterReader fruitMasterReader;
	
	/**
	 * fruitCategoryReader 水果分类读取接口
	 */
	private FruitCategoryReader fruitCategoryReader;
	
	/**
	 * <pre>
	 * TODO 请充分描述该构造函数的作用，及其参数的作用。
	 * </pre>
	 *   
	 */
	public FruitImpl(FruitMasterWriter fruitMasterWriter, 
			FruitNutritionWriter fruitNutritionWriter, 
			FruitArgumentWriter fruitArgumentWriter, 
			FruitStructureWriter fruitStructureWriter,
			FruitEfficacyRelationWriter fruitEfficacyRelationWriter,
			FruitNutritionReader fruitNutritionReader,
			NutrientReader nutritionReader,
			FruitArgumentReader fruitArgumentReader,
			ArgumentReader argumentReader,
			ArgumentCostReader argumentCostReader,
			FruitStructureReader fruitStructureReader,
			FruitEfficacyRelationReader fruitEfficacyRelationReader,
			EfficacyReader efficacyReader,
			FruitMasterReader fruitMasterReader,
			FruitCategoryReader fruitCategoryReader) {
		this.fruitMasterWriter = fruitMasterWriter;
		this.fruitNutritionWriter = fruitNutritionWriter;
		this.fruitArgumentWriter = fruitArgumentWriter;
		this.fruitStructureWriter = fruitStructureWriter;
		this.fruitEfficacyRelationWriter = fruitEfficacyRelationWriter;
		this.fruitNutritionReader = fruitNutritionReader;
		this.nutritionReader = nutritionReader;
		this.fruitArgumentReader = fruitArgumentReader;
		this.argumentReader = argumentReader;
		this.argumentCostReader = argumentCostReader;
		this.fruitStructureReader = fruitStructureReader;
		this.fruitEfficacyRelationReader = fruitEfficacyRelationReader;
		this.efficacyReader = efficacyReader;
		this.fruitMasterReader = fruitMasterReader;
		this.fruitCategoryReader = fruitCategoryReader;
	}

	/**
	 * @return
	 */
	public FruitInfo createFruitInfo() {
		return new FruitBean(fruitMasterWriter, fruitNutritionWriter, fruitArgumentWriter, fruitStructureWriter, fruitEfficacyRelationWriter, fruitNutritionReader, nutritionReader, fruitArgumentReader, argumentReader, argumentCostReader, fruitStructureReader, fruitEfficacyRelationReader, efficacyReader, fruitCategoryReader);
	}

	/**
	 * @return
	 */
	public FruitStructure createFruitStructureInfo() {
		return new FruitStructureBean(efficacyReader);
	}

	/**
	 * @param structureId
	 * @return
	 */
	public FruitStructure findFruitStructureInfoById(String structureId) {
		return null;
	}

	/**
	 * @param nutritionId
	 * @return
	 */
	public FruitNutrition createFruitNutritionInfo(String nutritionId) {
		NutritionEntity entity = nutritionReader.findNutrientById(nutritionId);
		return new FruitNutritionBean(new FruitNutritionEntity(), entity);
	}

	/**
	 * @param nutritionInfo
	 * @return
	 */
	public FruitNutrition createFruitNutritionInfo(NutritionBasic nutritionInfo) {
		NutritionEntity entity = new NutritionEntity(nutritionInfo.getId(), nutritionInfo.getName(), nutritionInfo.getDesc(), nutritionInfo.getSort(), nutritionInfo.getType());
		return new FruitNutritionBean(new FruitNutritionEntity(), entity);
	}
	
	/**
	 * @param fruitId
	 * @return
	 */
	public FruitInfo findFruitInfoById(String fruitId) {
		FruitEntity fruitEntity = fruitMasterReader.queryById(fruitId);
		return new FruitBean(fruitEntity, fruitMasterWriter, fruitNutritionWriter, fruitArgumentWriter, fruitStructureWriter, fruitEfficacyRelationWriter, fruitNutritionReader, nutritionReader, fruitArgumentReader, argumentReader, argumentCostReader, fruitStructureReader, fruitEfficacyRelationReader, efficacyReader, fruitCategoryReader);
	}
	
	/**
	 * @param startNo
	 * @param pageSize
	 * @return
	 */
	public PageResult<FruitInfo> findPageFruitInfo(Integer startNo, Integer pageSize) {
		
		PageQueryEntity<FruitEntity> result = fruitMasterReader.queryByPage(startNo, pageSize);
		PageQueryEntity<FruitInfo> pageResult = new PageQueryEntity<FruitInfo>(result.count(), new ArrayList<FruitInfo>(result.list().size()));
		for (FruitEntity fruitEntity : result.list()) {
			pageResult.list().add(new FruitBean(fruitEntity, fruitMasterWriter, fruitNutritionWriter, fruitArgumentWriter, fruitStructureWriter, fruitEfficacyRelationWriter, fruitNutritionReader, nutritionReader, fruitArgumentReader, argumentReader, argumentCostReader, fruitStructureReader, fruitEfficacyRelationReader, efficacyReader, fruitCategoryReader));
		}
		
		return pageResult;
	}
	
	/**
	 * @param fruitId
	 */
	public void remove(String fruitId) {
		
		// 删除水果主数据
		fruitMasterWriter.deleteByFruitId(fruitId);
		
		// 删除营养信息
		fruitNutritionWriter.deleteByFruitId(fruitId);
		
		// 删除结构信息
		fruitStructureWriter.deleteByFruitId(fruitId);
		
		// 删除水果功效信息
		fruitEfficacyRelationWriter.deleteByFruitId(fruitId);
		
		// 删除水果关联参数
		fruitArgumentWriter.deleteByFruitId(fruitId);
		
	}
	
	public PageResult<FruitInfo> searchPageFruitInfo(String type, String str, Integer startNo, Integer pageSize) {
		PageQueryEntity<FruitEntity> result = fruitMasterReader.searchByPage(type,str,startNo, pageSize);
		PageQueryEntity<FruitInfo> pageResult = new PageQueryEntity<FruitInfo>(result.count(), new ArrayList<FruitInfo>(result.list().size()));
		for (FruitEntity fruitEntity : result.list()) {
			pageResult.list().add(new FruitBean(fruitEntity, fruitMasterWriter, fruitNutritionWriter, fruitArgumentWriter, fruitStructureWriter, fruitEfficacyRelationWriter, fruitNutritionReader, nutritionReader, fruitArgumentReader, argumentReader, argumentCostReader, fruitStructureReader, fruitEfficacyRelationReader, efficacyReader, fruitCategoryReader));
		}
		return pageResult;
	}

}
